<?php

namespace Xin\ChatGPT\Channels;

use GuzzleHttp\Client;
use Xin\Capsule\WithConfig;
use Xin\ChatGPT\Contracts\Channel;

abstract class AbstractChannel implements Channel
{
    use WithConfig;

    /**
     * @var array
     */
    protected $config = [];

    /**
     * @var Client
     */
    protected $client = null;

    /**
     * @param array $config
     */
    public function __construct(array $config)
    {
        $this->config = $config;

        $this->createClient();
    }

    /**
     * @return void
     */
    protected function createClient()
    {
        $httpClient = new Client(array_replace_recursive(
            $this->getHttpClientDefaultOptions(),
            (array)$this->getConfig('http', [])
        ));

        $this->client = $httpClient;
    }

    /**
     * @return array<string, mixed>
     */
    protected function getHttpClientDefaultOptions(): array
    {
        return [];
    }

    /**
     * @return string
     */
    public function getDefaultModel(): string
    {
        return $this->getConfig('model', static::DEFAULT_MODEL);
    }

    /**
     * @return mixed
     */
    public function getDefaultTemperature()
    {
        return $this->getConfig('temperature', 0.3);
    }
}
